<?php
class ProductCatalogAction extends CommonAction {

	//private variable indexPath = "/Tpl/default/Product/Product_catalog/index.html" ;
	
	public $table = "product_catalog" ; 
	
	
	//框架首页
	public function index() {
		//按顺序查询产品类型				
		$model = D ( "product_type" );
		$condition['status'] = 1 ;
		$typeList = $model->where($condition)->order('createdate desc')->select();
		$this->assign("typeList",$typeList);
		//循环取出产品类型		
		$containList ;
		for ($i= 0;$i<count($typeList); $i++){
			$tempList = $typeList[$i];				
			$containList[$i] = $this->getSubFolder(0,$tempList['id'],$i) ;					
		}
		$this->assign("containList",$containList);		
		$this->display(APP_PATH . '/Tpl/default/ProductCatalog/index.html');
	}
	
	
	public function detail(){
		dump("----------" .$_REQUEST ['id'] ) ; 
		//$this->display(APP_PATH . '/Tpl/default/ProductCatalog/detail.html') ;
		$this->display();
	}
	

	private function getSubFolder($path,$type,$index){
		$str = "" ;		
		$subFolderCondition['childid'] = array('NEQ','0');
		$subFolderCondition['parentid'] = $path ;	
		$subFolderCondition['type_id'] = $type ;
		$subFolderCondition['status'] = 1 ;
		$subFolderList = $this->CommonSimplequery($subFolderCondition,'product_catalog');
		for ($i= 0;$i< count($subFolderList); $i++){				
				$tempList = $subFolderList[$i];
				//dump($tempList) ;						
				if($tempList['parentid'] == 0 ){
					$str = $str . "<ul class='tree treeFolder'>" ;	
				}else{
					$str = $str . "<ul>" ;
				}				
				$str = $str . "<li><a href='__URL__/detail/id/" . $tempList['id'] . "' target='sid_user' rel='".($index+1) ."'>" . $tempList['name']  . "</a>";
				//遍历子结点				
				$str = $str . $this->getSubFolder($tempList['id'],$type,$index) ;
  				$str = $str . $this->getSubDirect($tempList['id'],$type,$index) ;
				$str = $str . "</li>" ;		
				$str = $str . "</ul>" ;				
		} 		
		return $str;		
	}
	
	private function getSubDirect($path,$type){
		$str = "" ;
		$subDirectCondition['childid'] = '0';
		$subDirectCondition['parentid'] = $path ;
		$subFolderCondition['type_id'] = $type ;
		$subFolderCondition['status'] = 1 ;
		$subDirectList = $this->CommonSimplequery($subDirectCondition,'product_catalog');
		for ($i= 0;$i< count($subDirectList); $i++){
				$tempList = $subDirectList[$i];				
				$str = $str . "<ul>" ;
				$str = $str . "<li><a href='__URL__/detail/id/" . $tempList['id'] . "' target='ajax' rel='".($index+1)."'>" . $tempList['name'] . "</a></li>" ;	
				$str = $str . "</ul>" ;
		}
		return $str;		
	}	
	
	/**
	 * 
	 * 新增产品目录操作
	 */
	public function addCatalog(){
		
		$this->display();
	}
	
	/**
	 * 
	 * 保存产品目录
	 */
	public function saveCatalog(){
		$model = D( $this->table );	
		$data["name"] = $_REQUEST ['name'];		
		$data["childid"] = 1;
		$data["createdate"] = date('Y-m-d H:i:s',time());
		$data["status"] = $_REQUEST ['status'];
		
		$data["parentid"] = 3;		
		$data["type_id"] = 2;
		//$this->resultHandle($model->add($data));
		
		$save_result = $model->add($data) ;
		
		$type_model = D("product_type");
		$update_data['createdate'] =  date('Y-m-d H:i:s',time());
		$update_condition['id'] = 2 ; 
		$update_result = $type_model->where($update_condition)->save($update_data);
		
		
		if ( (false !== $save_result) && (false !== $update_result)) {
			$this->assign ( "jumpUrl","__APP__/ProductCatalog/detail" );			
			$this->success ( '状态修改成功！' );
		} else {
			$this->error ( '状态修改失败！' );
		}
	}
	
	/**
	 * 重载函数
	 * @see Admin/Lib/Action/CommonAction::getReturnUrl()
	 */
	function getCatalogReturnUrl() {
		return __URL__ . '?' .  C ( 'VAR_MODULE' ) . '=' . MODULE_NAME . '&' . C ( 'VAR_ACTION' ) . '=' . C ( 'DEFAULT_CATALOG_ACTION' );
		
	}

}
?>